package com.qf.springcloud.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.qf.springcloud.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author 水杯
 * @date 2021-01-04 08:42
 */
@RestController
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;//spring 提供的一个用于访问 rest 接口的模板对象
    @Autowired
    private EurekaClient eurekaClient;
    @Value("${user.url}")
    private String url;
    @GetMapping("/order/{id}")
    public User getOrder(@PathVariable  Long id) {
        //访问提供者,获取数据
        //  User user = restTemplate.getForObject(url + id, User.class);//通过访问 rest 获取到 json 数据,然后转换为 user 对象
       InstanceInfo instance = eurekaClient.getNextServerFromEureka("PROVIDER-USER", false);
        String homePageUrl = instance.getHomePageUrl();//请求地址
        User user = restTemplate.getForObject(homePageUrl+"/user/"+ id, User.class);
        return user;
    }
}
